package com.facebook.android;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.ilarele.shopping.client.R;
import com.ilarele.shopping.client.util.Global;

public class TestServerListener implements RequestListener {

	private Context mContext;
	private ProgressDialog mProgress;

	public TestServerListener(ProgressDialog prgr, Context context) {
		mProgress = prgr;
		mContext = context;
	}

	@Override
	public void onComplete(String response, Object state) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setCancelable(true).setPositiveButton(R.string.ok,
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
		try {
			String id = new JSONObject(response).getString("id");
			mProgress.cancel();

			if (id != null) {
				builder.setMessage(mContext.getString(R.string.post));
			} else {
				SessionStore.clear(mContext);
				builder.setMessage(mContext.getString(R.string.no_post));
				Global.facebook.logout(mContext);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		builder.show();
	}

	@Override
	public void onIOException(IOException e, Object state) {

	}

	@Override
	public void onFileNotFoundException(FileNotFoundException e, Object state) {
	}

	@Override
	public void onMalformedURLException(MalformedURLException e, Object state) {
	}

	@Override
	public void onFacebookError(FacebookError e, Object state) {
	}
}
